package com.lzugis.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lzugis.common.R;
import com.lzugis.entity.SysUser;
import com.lzugis.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName SysUserController
 * @Author lzugis
 * @Date 2020/9/16 11:32
 **/
@Api(tags = "用户管理")
@RestController
@RequestMapping("user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation("用户列表")
    @GetMapping("list")
    public R selectList() {
        return R.ok().put("list", sysUserService.selectList(new EntityWrapper<>()));
    }

    @ApiOperation("根据Id获取用户信息")
    @GetMapping("select/{id}")
    public R selectById(@PathVariable String id) {
        return R.ok().put("data", sysUserService.selectById(id));
    }

    @ApiOperation("根据Id删除用户")
    @GetMapping("delete/{id}")
    public R deleteById(@PathVariable String id) {
        return sysUserService.deleteById(id) ? R.ok() : R.error();
    }

    @ApiOperation("新增用户")
    @PostMapping("insert")
    public R insert(@RequestBody SysUser sysUser) {
        String md5Pwd = DigestUtils.md5DigestAsHex(sysUser.getLoginPwd().getBytes());
        sysUser.setLoginPwd(md5Pwd);
        return sysUserService.insert(sysUser) ? R.ok() : R.error();
    }

    @ApiOperation("更新用户")
    @PostMapping("update")
    public R update(@RequestBody SysUser sysUser) {
        return sysUserService.updateById(sysUser) ? R.ok() : R.error();
    }
}
